package com.vhall.component.service.rbac.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacMenuMapper;
import com.vhall.component.entity.account.MenuesEntity;
import com.vhall.component.entity.rbac.vo.AdminMenuListReqVo;
import com.vhall.component.entity.rbac.vo.AdminMenuListRspVo;
import com.vhall.component.service.rbac.RbacAdminMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xianglei
 * @date 2021/4/26 16:54
 */
@Service
public class RbacAdminMenuServiceImpl implements RbacAdminMenuService {

    @Autowired
    private RbacMenuMapper menuMapper;


    @Value("${vhall.menu.not_displayed}")
    private List<Integer> menuNotDisplayed;

    @Override
    public List<AdminMenuListRspVo> getList(AdminMenuListReqVo reqVo) {

        ArrayList<AdminMenuListRspVo> adminMenuListRspVos = new ArrayList<>();
        LambdaQueryWrapper<MenuesEntity> where = Wrappers.lambdaQuery();
        where.isNull(MenuesEntity::getDeletedAt);
        where.orderByAsc(MenuesEntity::getSort, MenuesEntity::getMenuId);
        if (reqVo.getStatus() != null) {
            where.eq(MenuesEntity::getStatus, reqVo.getStatus());
        }

        List<MenuesEntity> menuesEntities = menuMapper.selectList(where);

        if (CollectionUtil.isNotEmpty(menuesEntities) && CollectionUtil.isNotEmpty(menuNotDisplayed)) {
            menuesEntities = menuesEntities.stream().filter(e -> !menuNotDisplayed.contains(e.getMenuId())).collect(Collectors.toList());
        }


        AdminMenuListRspVo adminMenuListRspVo;
        for (MenuesEntity menuesEntity : menuesEntities) {
            adminMenuListRspVo = new AdminMenuListRspVo();
            BeanUtils.copyProperties(menuesEntity, adminMenuListRspVo);
            adminMenuListRspVo.setDisabled(menuesEntity.getStatus() == 2);
            adminMenuListRspVo.setDisabled(menuesEntity.getMenuId() == 1);
            if (menuesEntity.getType() == 1) {
                adminMenuListRspVo.setTypeName("admin菜单");
            } else {
                adminMenuListRspVo.setTypeName("控制台菜单");
            }
            adminMenuListRspVos.add(adminMenuListRspVo);
        }
        if (reqVo.getTree() == null || reqVo.getTree() == 0) {
            return adminMenuListRspVos;
        }

        ArrayList<AdminMenuListRspVo> treeOneList = new ArrayList<>();
        for (AdminMenuListRspVo menuListRspVo : adminMenuListRspVos) {
            if (menuListRspVo.getPid() == 0) {
                List treeList = getTreeList(adminMenuListRspVos, menuListRspVo.getMenuId());
                menuListRspVo.setChildren(treeList);
                if (menuListRspVo.getType() == 1) {
                    menuListRspVo.setTypeName("admin菜单");
                } else {
                    menuListRspVo.setTypeName("控制台菜单");
                }
                menuListRspVo.setDisabled(menuListRspVo.getStatus() == 2);
                treeOneList.add(menuListRspVo);
            }
            menuListRspVo.setDisabled(menuListRspVo.getStatus() == 2);
        }
        return treeOneList;
    }

    /**
     * 获取每个子模块
     *
     * @param
     * @return
     */
    private List<AdminMenuListRspVo> getTreeList(List<AdminMenuListRspVo> lists, Integer pid) {
        List<AdminMenuListRspVo> result = new ArrayList<>();
        lists.forEach(list -> {
            if (pid.equals(list.getPid())) {
                list.setDisabled(list.getStatus() == 2);
                List<AdminMenuListRspVo> child = getTreeList(lists, list.getMenuId());
                list.setChildren(child);
                result.add(list);
            }
        });
        return result;
    }
}
